package com.verbetia.mbrainz.tags;

import java.io.IOException;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


import com.verbetia.mbrainz.search.TrackSearch;


public class TagTrackReader {
	
	// http://ws.audioscrobbler.com/1.0/track/Metallica/Enter%20Sandman/toptags.xml
	static final String URL_PREFIX = "http://ws.audioscrobbler.com/1.0/track/";
	static final String URL_SUBFIX = "/toptags.xml";
	
	TagTackXMLReader lib = null;
	
	public static void main(String[] args) {
		
		TagTrackReader tr = new TagTrackReader("el burrito","divididos");
		ArrayList<TagItem> tags = tr.getTags();
		
		for (TagItem t : tags)
		{
			System.out.println("tag name: " + t.getName()+ " / Count : " + t.getCount());
			
		}
		
	}
	
	
	public ArrayList<TagItem> getTags()
	{
		return lib.getTags();
	}
	
	public TagTrackReader(String title, String artist)
	{
        String url = URL_PREFIX + artist + "/" + title + URL_SUBFIX;
        System.out.println(url);
        
        lib = new TagTackXMLReader();
     
		 try{
	            SAXParserFactory spf=SAXParserFactory.newInstance();
	            SAXParser sp = spf.newSAXParser();	            
	           	sp.parse(url, lib );
	        }catch(ParserConfigurationException e){
	        	System.err.println("error de  parseo");
	        }catch(SAXException e2){
	        	System.err.println("error de  sax : " + e2.getStackTrace());
	        } catch (IOException e3) {
				// TODO Auto-generated catch block
	        	System.err.println("error de  io : " + e3.getMessage() );
	        }

			
		
	}

private class TagTackXMLReader extends DefaultHandler {
  	
	 String contenido="";  // cadena para almacenar el contenido de un tag
	 TagItem item = null;
	 ArrayList<TagItem> tagItems = new ArrayList<TagItem>();
	 
	 
	 public ArrayList<TagItem> getTags()
	 {
		 return tagItems;
		 
	 }
	 
public void startDocument() throws SAXException{
	}
public void endDocument()throws SAXException{
	 	if (item!=null)
	 		tagItems.add(item);
	}

	/* Esta funcion el llamada cuando se produce el evento 
	 * de(non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String uri, String localName, String qName, Attributes attributes) {
	System.out.println("he encontrado una nueva etiqueta :" + qName);
	
		if (qName.equals("tag"))
		{	if (item!=null)
				tagItems.add(item);
			
			item = new TagItem();
		}			
   }

	/* Esta funcion es llamada cuando ve el contenido de un tag
	 * (non-Javadoc)
	 * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
	 */
   public void characters(char buf[], int offset, int len)
   throws SAXException
   {
   	contenido = new String(buf, offset, len);
   	System.out.println(contenido);
   }
   
   
   
   /* 
    * (non-Javadoc)
    * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
    */
   public void endElement(String uri, String localName, String qName) {
		if(qName.equals("name"))
			item.setName(contenido);

		if(qName.equals("count"))
			item.setCount(Integer.parseInt(contenido));	
    }
}
}